import models.crawlers.Muztorg;
import models.crawlers.common.ShopCrawler;
import models.entities.PictureEntity;
import models.entities.ProductEntity;
import models.helpers.ImageHelper;
import models.repository.ImageRepositorySql;
import models.repository.common.ImageRepository;
import org.junit.Test;
import play.test.FunctionalTest;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class Crawler_Tests extends FunctionalTest {
    @Test
    public void scan_test() throws Exception {
        ShopCrawler crawler = new Muztorg();
        ProductEntity product = crawler.getProduct("http://www.muztorg.ru/cat/details/D001430/");
        //assertEquals(product.title, "IBANEZ GART60 BLACK NIGHT");
        for (String url : product.pictureUrls) {
            BufferedImage data = getPictureData(url);
            String md5 = ImageHelper.getMD5HashCode(data);
            ImageRepository imageRepository = new ImageRepositorySql();
            PictureEntity dbImage = imageRepository.getImageByMD5(md5);
            assertNotNull(dbImage);
            //ImageHelper.saveImage(new java.net.URL(url), "~/Projects/DBSJ/data", "productimages");
        }
    }
    protected static BufferedImage getPictureData(String remoteUrl) throws IOException {
        if (remoteUrl == "")
            return null;
        java.net.URL url = new java.net.URL(remoteUrl);
        try {
            BufferedImage image = ImageIO.read(url);
            return image;
        } catch (IOException ex) {
            return null;
        }
    }
}
